package com.zyf.lj.controller;

import com.zyf.lj.entity.Application;
import com.zyf.lj.entity.College;
import com.zyf.lj.entity.Major;
import com.zyf.lj.service.ApplicationService;
import com.zyf.lj.service.CollegeService;
import com.zyf.lj.service.MajorService;
import com.zyf.lj.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 招生相关Controller
 */
@RestController
@RequestMapping("/api/enrollment")
public class EnrollmentController {
    
    @Autowired
    private CollegeService collegeService;
    
    @Autowired
    private MajorService majorService;
    
    @Autowired
    private ApplicationService applicationService;
    
    /**
     * 获取所有学院信息
     * @return 学院列表
     */
    @GetMapping("/colleges")
    public Result<List<College>> getAllColleges() {
        List<College> colleges = collegeService.getAllColleges();
        return Result.success(colleges);
    }
    
    /**
     * 获取所有学院信息及其专业
     * @return 包含专业信息的学院列表
     */
    @GetMapping("/colleges/with-majors")
    public Result<List<College>> getAllCollegesWithMajors() {
        List<College> colleges = collegeService.getAllCollegesWithMajors();
        return Result.success(colleges);
    }
    
    /**
     * 根据ID获取学院信息
     * @param id 学院ID
     * @return 学院信息
     */
    @GetMapping("/college/{id}")
    public Result<College> getCollegeById(@PathVariable Integer id) {
        College college = collegeService.getCollegeById(id);
        if (college != null) {
            return Result.success(college);
        } else {
            return Result.fail("学院不存在");
        }
    }
    
    /**
     * 根据ID获取学院信息及其专业
     * @param id 学院ID
     * @return 包含专业信息的学院
     */
    @GetMapping("/college/{id}/with-majors")
    public Result<College> getCollegeWithMajorsById(@PathVariable Integer id) {
        College college = collegeService.getCollegeWithMajorsById(id);
        if (college != null) {
            return Result.success(college);
        } else {
            return Result.fail("学院不存在");
        }
    }
    
    /**
     * 获取所有专业信息
     * @return 专业列表
     */
    @GetMapping("/majors")
    public Result<List<Major>> getAllMajors() {
        List<Major> majors = majorService.getAllMajors();
        return Result.success(majors);
    }
    
    /**
     * 根据ID获取专业信息
     * @param id 专业ID
     * @return 专业信息
     */
    @GetMapping("/major/{id}")
    public Result<Major> getMajorById(@PathVariable Integer id) {
        Major major = majorService.getMajorById(id);
        if (major != null) {
            return Result.success(major);
        } else {
            return Result.fail("专业不存在");
        }
    }
    
    /**
     * 根据学院ID获取专业列表
     * @param collegeId 学院ID
     * @return 专业列表
     */
    @GetMapping("/college/{collegeId}/majors")
    public Result<List<Major>> getMajorsByCollegeId(@PathVariable Integer collegeId) {
        List<Major> majors = majorService.getMajorsByCollegeId(collegeId);
        return Result.success(majors);
    }
    
    /**
     * 获取专业详细信息（包括课程和就业方向）
     * @param id 专业ID
     * @return 专业详细信息
     */
    @GetMapping("/major/{id}/detail")
    public Result<Major> getMajorDetailById(@PathVariable Integer id) {
        Major major = majorService.getMajorDetailById(id);
        if (major != null) {
            return Result.success(major);
        } else {
            return Result.fail("专业不存在");
        }
    }
    
    /**
     * 提交在线报名申请
     * @param application 报名申请对象
     * @return 提交结果
     */
    @PostMapping("/apply")
    public Result<Void> submitApplication(@RequestBody Application application) {
        // 验证必填字段
        if (application.getName() == null || application.getName().trim().isEmpty()) {
            return Result.fail("姓名不能为空");
        }
        if (application.getGender() == null || application.getGender().trim().isEmpty()) {
            return Result.fail("性别不能为空");
        }
        if (application.getIdCard() == null || application.getIdCard().trim().isEmpty()) {
            return Result.fail("身份证号不能为空");
        }
        if (application.getPhone() == null || application.getPhone().trim().isEmpty()) {
            return Result.fail("手机号不能为空");
        }
        if (application.getProvince() == null || application.getProvince().trim().isEmpty() ||
            application.getCity() == null || application.getCity().trim().isEmpty() ||
            application.getDistrict() == null || application.getDistrict().trim().isEmpty()) {
            return Result.fail("生源地信息不完整");
        }
        if (application.getSchool() == null || application.getSchool().trim().isEmpty()) {
            return Result.fail("毕业学校不能为空");
        }
        if (application.getEducation() == null || application.getEducation().trim().isEmpty()) {
            return Result.fail("学历类型不能为空");
        }
        if (application.getGraduationYear() == null || application.getGraduationYear().trim().isEmpty()) {
            return Result.fail("毕业年份不能为空");
        }
        if (application.getCollegeId() == null) {
            return Result.fail("报考学院不能为空");
        }
        if (application.getMajorId() == null) {
            return Result.fail("报考专业不能为空");
        }
        
        // 设置创建时间和更新时间
        Date now = new Date();
        application.setCreateTime(now);
        application.setUpdateTime(now);
        
        // 提交申请
        boolean success = applicationService.submitApplication(application);
        if (success) {
            return Result.success();
        } else {
            return Result.fail("提交申请失败，请稍后重试");
        }
    }
    
    /**
     * 根据身份证号查询报名申请
     * @param idCard 身份证号
     * @return 报名申请列表
     */
    @GetMapping("/application/id-card/{idCard}")
    public Result<List<Application>> getApplicationsByIdCard(@PathVariable String idCard) {
        List<Application> applications = applicationService.getApplicationsByIdCard(idCard);
        return Result.success(applications);
    }
    
    /**
     * 根据手机号查询报名申请
     * @param phone 手机号
     * @return 报名申请列表
     */
    @GetMapping("/application/phone/{phone}")
    public Result<List<Application>> getApplicationsByPhone(@PathVariable String phone) {
        List<Application> applications = applicationService.getApplicationsByPhone(phone);
        return Result.success(applications);
    }
    
    /**
     * 根据ID查询报名申请详情
     * @param id 申请ID
     * @return 报名申请详情
     */
    @GetMapping("/application/{id}")
    public Result<Application> getApplicationById(@PathVariable Integer id) {
        Application application = applicationService.getApplicationById(id);
        if (application != null) {
            return Result.success(application);
        } else {
            return Result.fail("申请不存在");
        }
    }
} 